package org.example.controller;


import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Emp;
import org.example.pojo.EmpQueryParam;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    /*
    * 分页查询
    * */
    /*@GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        PageResult<Emp> pageResult = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageResult);
    }*/

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询，参数：{},{},{},{},{},{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 增加用户信息
    * */
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("增加用户信息：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    /*
    * 删除用户信息
    * */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids)
    {
        log.info("删除用户信息：{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }


    /*
    * 编辑员工信息
    * */
    // 查询回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id)
    {
        log.info("根据id回显员工信息");
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    // 更新信息
    @PutMapping
    public Result update(@RequestBody Emp emp)
    {
        log.info("更新员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }

    // 查询所有员工信息
    @GetMapping("/list")
    public Result list()
    {
        log.info("查询所有员工信息");
        List<Emp> empList = empService.All();
        return Result.success(empList);
    }
}
